#######################################################
# 
# models.py
# Created on:      04-Jun-2008 9:37:29
# Original author: IdealLove
# 
#######################################################
from google.appengine.ext import db
from appengine_django.models import BaseModel
from django import newforms as forms
from google.appengine.api import users
from datetime import datetime

menu = ["Template"]

class DBModel(BaseModel):
	pass

class DBModelForm(forms.Form):
	def __init__(self,id=None, *args, **kwargs):
		self.id=id
		super(DBModelForm, self).__init__(*args, **kwargs)
	def connect(self, model=None):
		if self.id:
			dbObj= db.get(self.id)	
		else:
			dbObj = eval("%s()" % model)
#		print "hi"
		return dbObj

class Dated(DBModel):
    timecreated = db.DateTimeProperty(auto_now_add=True)
    timemodified = db.DateTimeProperty(auto_now=True)

class DatedForm(DBModelForm):
	timecreated = forms.DateTimeField(label="Time Created", initial=datetime.utcnow(),required=False,
		widget=forms.DateTimeInput(attrs={'disabled':'disabled'}))
	timemodified = forms.DateTimeField(label="Time Modified", required=False,
		widget=forms.DateTimeInput(attrs={'disabled':'disabled'}))
	def save(self, dbObj=None):
		if (not dbObj):
			dbObj = super(DatedForm, self).connect(model="Authored")
		dbObj.put()
		return dbObj


class Template(Dated):
	name = db.StringProperty()
	templatetype = db.StringProperty()
	collection = db.StringProperty()
	content = db.TextProperty()

TEMPLATETYPE_CHOICES = [
	('content', 'content'),
	('sideblock', 'sideblock'),
	('header/footer', 'header/footer'),
	('wrapper', 'wrapper'),
]
	

class TemplateForm(DatedForm):
	name = forms.CharField()
	templatetype = forms.ChoiceField(choices=TEMPLATETYPE_CHOICES)
	collection = forms.CharField(initial="default")
	content = forms.CharField(widget=forms.Textarea(attrs={'class':'mceEditor'}))
	def save(self, dbObj=None):
		if (not dbObj):
			dbObj = super(TemplateForm, self).connect(model="Template")
		dbObj.name = self.cleaned_data['name']
		dbObj.templatetype = self.cleaned_data['templatetype']
		dbObj.collection = self.cleaned_data['collection']
		dbObj.content = self.cleaned_data['content']
		dbObj.put()
		return dbObj












